package com.example.a79273.shandianban.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.adapter.XianHuoListAdapter;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.config.NetConfig_DesignerInfo;
import com.example.a79273.shandianban.model.XianHuoBean;

import org.xutils.HttpManager;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 79273 on 2016/9/1.
 */
public class Product extends Fragment {

    private TextView name_tv, fansCount_tv;
    private ImageView pic_iv;
    private RadioButton xianHuo_rb, shouGao_rb;
    private ListView listView;

    private HttpManager httpManager = x.http();
    private HashMap<String, List<XianHuoBean.DataBean.ListsBean>> listData;

    private XianHuoListAdapter listAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product, container, false);
        initView(view);
        initData();
        setData();
        return view;
    }

    private void setData() {
        listView.setAdapter(listAdapter);
    }

    private void initData() {

        listData = new HashMap<>();
        listAdapter = new XianHuoListAdapter(getActivity(), listData);
        loadData(String.format(NetConfig_DesignerInfo.XIANHUO_URL,"234001674942"));
    }

    private void initView(View view) {
        pic_iv = (ImageView) view.findViewById(R.id.iv_product_pic);
        name_tv = (TextView) view.findViewById(R.id.tv_product_name);
        fansCount_tv = (TextView) view.findViewById(R.id.tv_product_fansCount);
        xianHuo_rb = (RadioButton) view.findViewById(R.id.rb_product_xianHuo);
        shouGao_rb = (RadioButton) view.findViewById(R.id.rb_product_shouGao);
        listView = (ListView) view.findViewById(R.id.lv_product_product);
    }

    public void loadData(String url) {
        httpManager.get(new RequestParams(url), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XianHuoBean bean = XianHuoBean.objectFromData(MyApplication.dealWithString(result));
                if (bean != null) {

                    List<XianHuoBean.DataBean.ListsBean> list = bean.getData().getLists();
                    //用于判断是否已存相同fatherName的数据
                    Map<String, XianHuoBean.DataBean.ListsBean> temp = new HashMap<>();
                    List<XianHuoBean.DataBean.ListsBean> itemData;
                    String fatherName = null;
                    for (int i = 0; i < list.size(); i++) {
                        XianHuoBean.DataBean.ListsBean listsBean = list.get(i);
                        fatherName = listsBean.getFathername();
                        XianHuoBean.DataBean.ListsBean put = temp.put(fatherName, listsBean);
                        if (put == null) {
                            itemData = new ArrayList<>();
                            listData.put(fatherName, itemData);
                        }
                        listData.get(fatherName).add(listsBean);
                    }
                    listAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
